<?php
namespace Clover\Controller;

use Think\Controller;

class BooksController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        session('menu', "books");
    }

    /**
     * 产品列表 显示分类id=1的 产品列表
     * @param int cat_id 默认值1
     */
    public function index()
    {
        check_privilege();
        session('menu_sub', "books_list");
        $cat_id = I("get.cat_id", 0);
        $category_list = M("Books")->table("dsk_books_category")->order(array('sort_order' => "desc", 'id' => "asc"))->select();
        infiniteAssort($category_list, 0, 0, "parent_id", "id", $result, "p_level");
        $books_list = D("Books")->get_books_list($cat_id);
        $data = array(
            "category_list" => $result,
            "books_list" => $books_list,
            "cat_id" => $cat_id,
            "menu" => "news"
        );
        $this->assign($data);
        $this->display("books_list");
    }

    /**
     * 跳转到添加页面
     * @return [type] [description]
     */
    public function add_Book()
    {
        check_privilege();
        $Books_id = I("get.id", 0);
        $category_list = M("Books")->table("dsk_books_category")->order(array('sort_order' => "desc", 'id' => "asc"))->select();
        infiniteAssort($category_list, 0, 0, "parent_id", "id", $result, "p_level");
        $data = array(
            "Books_id" => $Books_id,
            "category_list" => $result,
        );
        $this->assign($data);
        $this->display("book_add");
    }

    /**
     * 执行产品添加操作
     */
    public function do_add()
    {
        $book_name = I("post.book_name", "");
        $cat_id = I("post.cat_id", 0);
        $book_price = I("post.book_price", 0);
        $book_base_price = I("post.book_base_price", 0);
        $book_press = I("post.book_press", 0);
        $book_press_time = I("post.book_press_time", 0);
        $book_page = I("post.book_page", "");
        $book_size = I("post.book_size", "");
        $book_isbn = I("post.book_isbn", "");
        $sort_order = I("post.sort_order", 0);
        $book_link = I("post.book_link", "");
        $book_img = I("post.book_img", "");
        $book_author = I("post.book_author", "");
        $book_intro = I("post.book_intro", "", false);
        $Books = M("Books");
        $data = array(
            "book_name" => $book_name,
            "cat_id" => $cat_id,
            "book_price" => $book_price,
            "book_base_price" => $book_base_price,
            "book_press" => $book_press,
            "book_press_time" => $book_press_time,
            "book_page" => $book_page,
            "book_size" => $book_size,
            "book_isbn" => $book_isbn,
            "sort_order" => $sort_order,
            "book_intro" => $book_intro,
            "book_author" => $book_author,
            "book_img" => $book_img,
            "book_link" => $book_link,
            "add_time" => time()
        );
        $res = $Books->data($data)->add();
        echo $res;
    }

    /**
     * 跳转到修改页面
     * @return [type] [description]
     */
    public function edit_Book()
    {
        check_privilege();
        $book_id = I("get.id", 0);
        $category_list = M("Books")->table("dsk_books_category")->order(array('sort_order' => "desc", 'id' => "asc"))->select();
        infiniteAssort($category_list, 0, 0, "parent_id", "id", $result, "p_level");
        $book_info = M("Books")->where("id=" . $book_id)->find();
        $data = array(
            "book_id" => $ook_id,
            "category_list" => $result,
            "book_info" => $book_info,
            "add_time" => time()
        );
        $this->assign($data);
        $this->display("book_edit");
    }

    /**
     * 执行产品修改操作
     */
    public function do_edit()
    {
        $id = I("post.id", 0);
        $book_name = I("post.book_name", "");
        $cat_id = I("post.cat_id", 0);
        $book_price = I("post.book_price", 0);
        $book_base_price = I("post.book_base_price", 0);
        $book_press = I("post.book_press", 0);
        $book_press_time = I("post.book_press_time", 0);
        $book_page = I("post.book_page", "");
        $book_size = I("post.book_size", "");
        $book_isbn = I("post.book_isbn", "");
        $sort_order = I("post.sort_order", 0);
        $book_link = I("post.book_link", "");
        $book_img = I("post.book_img", "");
        $book_author = I("post.book_author", "");
        $book_intro = I("post.book_intro", "", false);
        $Books = M("Books");
        $data = array(
            "book_name" => $book_name,
            "cat_id" => $cat_id,
            "book_price" => $book_price,
            "book_base_price" => $book_base_price,
            "book_press" => $book_press,
            "book_press_time" => $book_press_time,
            "book_page" => $book_page,
            "book_size" => $book_size,
            "book_isbn" => $book_isbn,
            "sort_order" => $sort_order,
            "book_intro" => $book_intro,
            "book_author" => $book_author,
            "book_img" => $book_img,
            "book_link" => $book_link,
            "add_time" => time()
        );
        $res = $Books->where("id=" . $id)->save($data);
        echo $res;
    }

    /**
     * 执行产品删除
     * @return [type] [description]
     */
    public function do_delete()
    {
        $id = I("post.id", 0);
        //删除图片文件
        $book_info = M("Books")->where("id=$id")->find();
        $res = 0;
        if (!empty($book_info)) {
            $src = __ROOT__ . 'Public/Uploads/' . $book_info['banner_img'];
            if (file_exists($src)) {
                unlink($src);
                $res = M("Books")->where("id=$id")->delete();
            }
        }
        echo $res;
    }

    /**
     * 产品分类列表页面
     *
     */
    public function category_list()
    {
        check_privilege();
        session('menu_sub', "books_cat");
        $Books = M("Books");
        $category_list = $Books->table("dsk_books_category")->order(array('sort_order' => "desc", 'id' => "asc"))->select();
        infiniteAssort($category_list, 0, 0, "parent_id", "id", $result, "p_level");
        $data = array(
            "category_list" => $result
        );
        $this->assign($data);
        $this->display("category_list");
    }

    /**
     * 跳转到分类信息添加页面
     */
    public function add_category()
    {
        check_privilege();
        $Books = M("Books");
        $category_list = $Books->table("dsk_books_category")->order(array('sort_order' => "desc", 'id' => "asc"))->select();
        infiniteAssort($category_list, 0, 0, "parent_id", "id", $result, "p_level");
        $data = array(
            "category_list" => $result
        );
        $this->assign($data);
        $this->display("category_add");
    }

    public function do_add_category()
    {
        $parent_id = I("post.parent_id", 0);
        $cat_name = I("post.cat_name", "");
        $sort_order = I("post.sort_order", 0);
        $Books = D("Books");
        $data = array(
            "parent_id" => $parent_id,
            "cat_name" => $cat_name,
            "sort_order" => $sort_order,
            "add_time" => time()
        );
        $res = $Books->table("dsk_books_category")->data($data)->add();
        echo $res;
    }

    /**
     * 跳转到分类信息修改页面
     */
    public function edit_category()
    {
        check_privilege();
        $id = I("get.id", 0);
        $category_list = M("Books")->table("dsk_books_category")->order(array('sort_order' => "desc", 'id' => "asc"))->select();
        infiniteAssort($category_list, 0, 0, "parent_id", "id", $result, "p_level", 1, array('id' => $id));
        $category_info = M("Books")->table("dsk_books_category")->where("id=" . $id)->find();
        $data = array(
            "category_list" => $result,
            "category_info" => $category_info,
            "cat_id" => $id
        );
        $this->assign($data);
        $this->display("category_edit");
    }

    public function do_edit_category()
    {
        $cat_name = I("post.cat_name", "");
        $sort_order = I("post.sort_order", 0);
        $id = I("post.id", 0);

        $Books = D("Books");
        $data = array(
            "cat_name" => $cat_name,
            "sort_order" => $sort_order
        );
        $res = $Books->table("dsk_books_category")->where("id=" . $id)->save($data);
        echo $res;
    }

    /**
     * 执行产品分类删除同时删除旗下子集
     * @return [type] [description]
     */
    public function do_delete_category()
    {
        $cat_id = I("post.id", 0);
        $del_ids = $cat_id;
        $childrenIds = D("Books")->get_Children_Ids($cat_id);
        if ($childrenIds) {
            $del_ids .= $childrenIds;
        }
        $Books = M("Books");
        $map['id'] = array('in', $del_ids);
        $status = $Books->table("dsk_books_category")->where($map)->delete();
        echo $status;
    }

    //上传cover
    public function upload_cover()
    {
        $upload = new \Think\UploadFile();// 实例化上传类
        $upload->maxSize = 5242880;// 5M设置附件上传大小
        $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $savepath = './Public/Uploads/' . date('Y-m-d') . '/';
        if (!file_exists($savepath)) {
            mkdir($savepath);
        }
        $upload->savePath = $savepath;// 设置附件上传目录
        if (!$upload->upload()) {// 上传错误提示错误信息
            echo json_encode(array('code' => 0, 'message' => '上传失败'));
        } else {// 上传成功 获取上传文件信息
            $info = $upload->getUploadFileInfo();
            $file_name = date('Y-m-d') . "/" . $info[0]['savename'];
            echo $file_name;
        }
    }

    /**
     * 执行banner删除操作
     */
    public function do_delete_pic()
    {
        $id = I("post.id", 0);
        $book_info = M("Books")->where("id=$id")->find();
        $res = 0;
        if (!empty($book_info)) {
            $src = __ROOT__ . 'Public/Uploads/' . $book_info['banner_img'];
            if (file_exists($src)) {
                unlink($src);
                $data = array(
                    "book_img" => null
                );
                $res = M("Books")->where("id=$id")->save($data);
            }
        }
        echo $res;
    }
}